package com.kk.my_pro.config.security;


import jakarta.servlet.*;
import org.springframework.security.authentication.TestingAuthenticationToken;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

import java.io.IOException;

/**
 * 自定义过滤链
 */
public class MyCustomFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        System.out.println("before.....");


        SecurityContext context = SecurityContextHolder.createEmptyContext();

        TestingAuthenticationToken testingAuthenticationToken
                = new TestingAuthenticationToken("username", "password", "ROLE_USER");

        context.setAuthentication(testingAuthenticationToken);

        SecurityContextHolder.setContext(context);


        filterChain.doFilter(servletRequest,servletResponse);

        System.out.println("after");
    }
}
